package com.atguigu.crud.controller;

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.bean.Msg;
import com.atguigu.crud.bean.Recordsinfo;
import com.atguigu.crud.service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息记录（系统记录，转帐记录，收款记录）
 * @author lfy
 *
 */
@CrossOrigin
@Controller
@RequestMapping("/rec")
public class RecordsController {
	@Autowired
	private RecordsService recordsService;
	/*条件查询消息记录
	*1、收款2、转帐3系统记录、4交易记录
	*/
	@RequestMapping("/getAll")
	@ResponseBody
	public Msg getAll(Recordsinfo recordsinfo){
		return Msg.success().add("list", recordsService.getAllByPars(recordsinfo));
	}
	/**
	 * 2、根据id查询
	 * @PathVariable("id") 获取动态参数
	 * @param id
	 * @return
	 * @PathVariable 映射 URL 绑定的占位符
	 */
	@RequestMapping(value="/record/{id}",method= RequestMethod.GET)
	@ResponseBody
	public Msg getEmp(@PathVariable("id")String id){
		return Msg.success().add("recordsinfo", recordsService.getRecord(id));
	}
	/**
	 * 3、保存
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public Msg save(@RequestBody Recordsinfo recordsinfo){
		try {
			recordsService.saveRec(recordsinfo);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail().add("error",e.getMessage());
		}
		return Msg.success();
	}
}
